package org.lanqiao.music.web.controller;

import com.github.pagehelper.PageInfo;
import org.lanqiao.music.common.dto.MusicDTO;
import org.lanqiao.music.common.utils.PageDTO;
import org.lanqiao.music.common.utils.RateLimit;
import org.lanqiao.music.common.utils.Result;
import org.lanqiao.music.system.entity.Music;
import org.lanqiao.music.system.service.MusicService;
import org.lanqiao.music.system.vo.MusicLyricsVo;
import org.lanqiao.music.system.vo.MusicUrlVo;
import org.lanqiao.music.system.vo.MusicVO;
import org.lanqiao.music.system.vo.HotMusicVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/api/music")
public class MusicController {
    @Autowired
    private MusicService musicService;

    @GetMapping("/getById")
    public MusicVO getMusicVOById(Integer musicId) {
        return musicService.queryById(musicId);
    }

    //分页查询全部歌曲
    @GetMapping("/all")
    public PageInfo<Music> getMusic() {
        return musicService.queryByPage(new Music(),new PageDTO());
    }

    // 根据关键字搜索音乐信息
    @RateLimit(limit = 5, timeout = 60) // 60秒内最多允许调用5次
    @GetMapping("/search")
    public Result searchMusic(MusicDTO dto){
        Result result = Result.success(musicService.searchMusic(dto));
        return result;
    }

    //新增
    @PostMapping("/insert")
    public Result insert(@RequestBody Music music){
        int r = musicService.insert(music);
        if(r == 1){
            return Result.success("添加成功");
        }
        return Result.fail("添加失败");
    }

    //批量新增
    @PostMapping("/batchInsert")
    public Result insertBatch(@RequestBody List<Music> musicList) {
        int r = musicService.insertBatch(musicList);
        return Result.success(r);
    }

    //删除
    @PostMapping("/del")
    public Result deleteById(Integer musicId){
        int r = musicService.deleteById(musicId);
        if(r == 1){
            return Result.success();
        }
        return Result.fail();
    }

    //批量删除
    @PostMapping("/batchDelete")
    public Result deleteBatch(@RequestBody List<Long> musicIds) {
        int r = musicService.deleteBatch(musicIds);
        return Result.success(r);
    }

    /**
     * 更新音乐信息
     *
     * @param music Music 实例对象
     * @return 更新结果
     */
    @PutMapping("/update")
    public Result updateMusic(@RequestBody Music music) {
        int r = musicService.update(music);
        if (r > 0) {
            return Result.success("更新成功");
        } else {
            return Result.fail("更新失败");
        }
    }

    @GetMapping("/lyrics")
    public MusicLyricsVo getLyricsByMusicId(Integer musicId) {
        MusicLyricsVo lyricsVO = new MusicLyricsVo();
        lyricsVO.setMusicLyrics(musicService.getLyricsByMusicId(musicId));
        return lyricsVO;
    }

    @GetMapping("/MusicUrl")
    public MusicUrlVo getMusicFilePathByMusicId(Integer musicId) {
        System.out.println("音乐文件接口");
        MusicUrlVo musicUrlVO = new MusicUrlVo();
        musicUrlVO.setMusicUrl(musicService.getMusicFilePathByMusicId(musicId));
        return musicUrlVO;
    }

    @PostMapping("/hotMusicAdd")
    public void hotMusicAdd(Integer musicId){
        System.out.println("热点数据接口");
        musicService.hotMusicAdd(musicId);
    }

    @GetMapping("/getHotMusic")
    public List<HotMusicVo> hotMusic(){
       return musicService.getHotMusic();
    }
}
